/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-31 11:40:33
 * @Description  : 用于退出登录的hooks逻辑抽离
 */

import { useMessage } from 'naive-ui'
import useUserStore from '@/stores/modules/user'
import { userLoginOut } from '@/api/user'
import { load } from '@/utils/loading'

export const useLogout = () => {
  const message = useMessage()
  const router = useRouter()
  const userStore = useUserStore()

  const handleLogout = () => {
    load.show()
    userLoginOut()
      .then(() => {
        message.success('退出登录成功')
        // 清除token
        userStore.removeToken()
        userStore.initUserStore()
        
      })
      .catch(console.error)
      .finally(() => {
        // 调整到登录页面
        router.replace({
          name: 'login'
        })
        load.hide()
      })
  }

  return {
    handleLogout
  }
}
